﻿<%@ WebHandler Language="C#" Class="QuestionHandler" %>

using System;
using System.Web;
using System.Collections.Generic;

using EHSubmit.Logic;
using EHSubmit.Model;
using EHSubmit.Util;

using EHSubmit.Logic.LINQ;

public class QuestionHandler : EHSubmit.Logic.BaseAjaxHandler {
    protected override void Mapping()
    {
        OprationMap["List_Institute"] = "FindInstitutes";            //获取学院信息
        OprationMap["List_Depts"] = "FindDepartmentsOfInstitute";    //查找相关学院的专业信息

        OprationMap["addQes"] = "Add";                               //添加习题
        OprationMap["listQes"] = "ListQuestions";                    //列出习题
        OprationMap["searchQes"] = "SearchQuestions";                //按条件搜索习题
        OprationMap["removeQes"] = "RemoveQuestion";                 //删除习题
    }

    /// <summary>
    /// 获取学院信息
    /// </summary>
    public void FindInstitutes()
    {
        if (Validate(ERole.Teacher))
        {
            InstituteManager istManager = new InstituteManager();
            IList<Institute> istList = new List<Institute>();
            string istJson = null;

            istList = istManager.GetInstitutes();
            if (istList.Count != 0)
            {
                istJson = JsonHelper.Serialize(istList);

                IsSuccess = true;
                ResponseText = "{" + string.Format("institutes:{0}", istJson) + "}";
            }
            else
            {
                Message = "无学院信息!";
            }
        }
        else
        {
            Message = Constants.INFO_NO_ACCESS;
        }
    }

    /// <summary>
    /// 查找相关学院的专业信息
    /// </summary>
    public void FindDepartmentsOfInstitute()
    {

        if (Validate(ERole.Teacher))
        {
            DepartmentManager deptManager = new DepartmentManager();
            IList<Department> deptList = new List<Department>();
            string deptJson = null;

            //---学院id
            int insID = WebHelper.IntSafeConvert(Context.Request["insID"], -1);
            if (insID != -1)
            {
                deptList = deptManager.GetDepartmentByInstituteID(insID);
                if (deptList.Count != 0)
                {
                    deptJson = JsonHelper.Serialize(deptList);

                    IsSuccess = true;
                    ResponseText = "{" + string.Format("total:{0},depts:{1}", deptList.Count, deptJson) + "}";
                }
                else
                {
                    IsSuccess = true;
                    ResponseText = "{" + string.Format("total:{0},depts:[]", deptList.Count) + "}";
                }
            }
            else
            {
                Message = Constants.INFO_UNKNOWN_ERR;
            }
        }
        else
        {
            Message = Constants.INFO_NO_ACCESS;
        }
    }

    /// <summary>
    /// 添加习题
    /// </summary>
    public override void Add()
    {
        if (Validate(ERole.Teacher))
        {
            QuestionManager qesManager = new QuestionManager();
            Question qes = new Question();
            IsSuccess = true;
            
            qes.OwnerID = CurrentUser.ID;
            qes.Type = WebHelper.IntSafeConvert(Context.Request["qesTypeRadio"],-1);

            //----单项选择情况
            if (qes.Type == 0)
            {
                qes.Answer = Context.Request["singleAns"];
            }
            //---多项选择情况-------
            else if(qes.Type ==1)
            {
                string multiAns = Context.Request["multiAns"];
                IList<string> ans = multiAns.Split(',');
                //---答案格式形如ABCD
                for (int i = 0; i < ans.Count; i++)
                {
                    qes.Answer += ans[i];
                }
            }
            else
            {
                IsSuccess = false;
            }

            string qesContent = Context.Request["qesContent"].Trim();
            qes.Content = qesContent.Replace("\n","<br>");
            
            qes.UpdateTime = DateTime.Now;
            qes.RightCount = 0;
            qes.WrongCount = 0;
            qes.Rate = 0;
            qes.BaseCourseID = Context.Request["BaseCourseID"];     //真正的课程号，不是课程班级ID

            //qes.Institute = WebHelper.IntSafeConvert(Context.Request["institute"], -1);
            //qes.Department = WebHelper.IntSafeConvert(Context.Request["department"], -1);
            //if (qes.Institute == -1 || qes.Department == -1)
            //{
            //    IsSuccess = false;
            //}

            qes.Tag = string.Empty;   //暂不处理  
                
            IList<Options> optionsList = JsonHelper.Deserialize<IList<Options>>(Context.Request["opts"]);

            if (IsSuccess)
            {
                int qid = qesManager.AddQuestion(qes, optionsList);
                //成功插入后返回习题
                object q = qesManager.GetQuestion(qid);
                string data = JsonHelper.Serialize(q);

                ResponseText = "{" + string.Format("success:true,msg:'添加成功!',data:{0}",data) + "}";
            }
            else
            {
                Message = Constants.INFO_UNKNOWN_ERR;
            }
        }
        else
        {
            Message = Constants.INFO_NO_ACCESS;
        }
    }

    /// <summary>
    /// 列出客观题
    /// </summary>
    public void ListQuestions()
    {
        int goalId = int.Parse(Context.Request["goalId"]);
        EHSubmit.Logic.LINQ.QuestionManager manager = new EHSubmit.Logic.LINQ.QuestionManager();

        Object ques = manager.GetQuestions(goalId);

        string data = JsonHelper.Serialize(ques);
        IsSuccess = true;
        ResponseText = "{" + string.Format("success:true, data:{0}", data) + "}";
    }

    /// <summary>
    /// 按条件搜索习题
    /// </summary>
    public void SearchQuestions()
    {
        if (Validate(ERole.Teacher))
        {
            string baseCouseID = Context.Request["baseCourseID"];
            
            QuestionManager qesManager = new QuestionManager();
            object qes = qesManager.GetQuestionsByCondition(baseCouseID, CurrentUser.ID);

            string qesJson = JsonHelper.Serialize(qes);
            IsSuccess = true;
            ResponseText = "{" + string.Format("success:true, data:{0}", qesJson) + "}";
            
        }
        else
        {
            Message = Constants.INFO_NO_ACCESS;
        }
    }

    /// <summary>
    /// 删除习题
    /// </summary>
    public void RemoveQuestion()
    {
        if (Validate(ERole.Teacher))
        {
            int qesID = WebHelper.IntSafeConvert(Context.Request["qesID"],-1);
            if (qesID != -1)
            {
                QuestionManager qesManager = new QuestionManager();
                qesManager.DeleteQuestion(qesID);

                IsSuccess = true;
                ResponseText = "{" + string.Format("success:true,msg:\"{0}\"","已成功删除!") + "}";
            }
            else
            {
                Message = Constants.INFO_UNKNOWN_ERR;
            }

        }
        else
        {
            Message = Constants.INFO_NO_ACCESS;
        }
    }
}